package com.wrox.todolist;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TodoItemArrayAdapter extends ArrayAdapter<TodoItem> {

	private int resource;

	public TodoItemArrayAdapter(Context context, int textViewResourceId,
			List<TodoItem> objects) {
		super(context, textViewResourceId, objects);
		this.resource = textViewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout v;
		
		if (convertView != null) {
			v = (LinearLayout) convertView;
		}
		else {
			v = new LinearLayout(getContext());
			LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(resource, v, true);
		}
		TextView dateView = (TextView) v.findViewById(R.id.rowDate);
		TodoItemView itemView = (TodoItemView) v.findViewById(R.id.row);
		
		TodoItem item = getItem(position);
		dateView.setText(item.getCreatedString());
		itemView.setText(item.getTask());
		itemView.setFinished(item.isFinished());
		
		return v;
	}

}
